package gameObjects;

import java.io.IOException;

import arena.Arena;

import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
/**
 * <code>Classe che carica il modello3D della moto e applica le texture.
 * Il modello caricato viene inviato alla superclasse
 * {@link Object3D} che gestisce tutto il modello nel gioco.
 * La classe deriva da {@link Bike}</code>
 * 
 * @author Massimiliano Palermo
 * @version 0.9b
 *
 */
public class CopyOfTank extends Object3D{
	private static final long serialVersionUID = 1904101613L;
	private int color;
	/**
	 * Costruttore
	 * @param name: {@link String} nome dell'oggetto da creare
	 * @param color: {@link Integer} colore del modello
	 * @param modelPath: {@link String} percorso del modello da caricare
	 * @throws IOException
	 */
	public CopyOfTank(String name,int color,String modelPath) throws IOException
	{
		super(name);
		super.model3D =  ((Node)this.getModel(modelPath));
		this.color=color;
		this.setNormalizeObject();
		this.setMaterial();
		this.attachChild(super.model3D);
	}
	
	public int getColor()
	{
		return color;
	}
	
	private void setMaterial()
	{
		Material mat = new Material();
		Node node    = new Node();
		
		/*Spatial sp = super.model3D.getChild("MOTO");
		List<Spatial> root = ((Node)sp).getChildren();
		for(int i=0;i<root.size();i++)
		{
			System.out.println("CHILD: " + root.get(i).getName().toString());
		}*/
		
		node = (Node) super.model3D.getChild(0);
		mat.setMaterial(Arena.colorAmb);
		node.setRenderState(mat.getMaterial());
		
		node = (Node) super.model3D.getChild("MOTO");
		node = (Node) node.getChild("CORPO");
		mat.setMaterial(Arena.colorAmb);
		node.setRenderState(mat.getMaterial());
		
		node = (Node) this.model3D.getChild("MOTO");
		node = (Node) node.getChild("VISORE");
		mat.setMaterial(Arena.colorAmb);
		node.setRenderState(mat.getMaterial());
		
		node = (Node) this.model3D.getChild("RA");
		node = (Node) node.getChild("G1");
		mat.setMaterial(Arena.colorAmb);
		node.setRenderState(mat.getMaterial());
		
		node = (Node) this.model3D.getChild("RA");
		node = (Node) node.getChild("C1");
		mat.setMaterial(Arena.colorAmb);
		node.setRenderState(mat.getMaterial());
		
		node = (Node) this.model3D.getChild("RD");
		node = (Node) node.getChild("G2");
		mat.setMaterial(Arena.colorAmb);
		node.setRenderState(mat.getMaterial());
		
		node = (Node) this.model3D.getChild("RD");
		node = (Node) node.getChild("C2");
		mat.setMaterial(Arena.colorAmb);
		node.setRenderState(mat.getMaterial());
	}
	
	/**
	 * Funzione che ridimensiona il modello 3D
	 * @return {@link Spatial}: modello 3D
	 */
	public Spatial setNormalizeObject()
	{
		super.model3D.setLocalScale(0.0015f);
        return super.model3D;
	}
	
	/**
	 * Procedura che applica la texture al modello 3D
	 */
	/*public void setTexture()
	{
		Node node = new Node();
		TextureMapping tex = new TextureMapping(display);
		
		node = (Node) super.model3D.getChild("MOTO");
		node = (Node) node.getChild("CORPO");
		tex.setTexture("textures/blue_color.jpg", new Vector3f(1, 1, 1), true);
		tex.setTextureManager(node);
		
		node = (Node) this.model3D.getChild("MOTO");
		node = (Node) node.getChild("VISORE");
		tex.setTexture("textures/black_color.jpg", new Vector3f(1, 1, 1), true);
		tex.setTextureManager(node);
		
		node = (Node) this.model3D.getChild("RA");
		node = (Node) node.getChild("G1");
		tex.setTexture("textures/black_color.jpg", new Vector3f(1, 1, 1), true);
		tex.setTextureManager(node);
		
		node = (Node) this.model3D.getChild("RA");
		node = (Node) node.getChild("C1");
		tex.setTexture("textures/blue_opaque_color.jpg", new Vector3f(1, 1, 1), true);
		tex.setTextureManager(node);
		
		node = (Node) this.model3D.getChild("RD");
		
		node = (Node) node.getChild("G2");
		tex.setTexture("textures/black_color.jpg", new Vector3f(1, 1, 1), true);
		tex.setTextureManager(node);
		
		node = (Node) this.model3D.getChild("RD");
		node = (Node) node.getChild("C2");
		tex.setTexture("textures/blue_opaque_color.jpg", new Vector3f(1, 1, 1), true);
		tex.setTextureManager(node);
	}*/
	
	/**
	 * Funzione che carica il modello 3D memorizzato nella
	 * superclasse {@link Bike}
	 * @return {@link Spatial}: il modello 3D
	 * @throws IOException
	 */
	public Spatial getModel(String path) throws IOException
	{
		Spatial model = super.getModel(path);
		return model;
	}
	
	public void WheelMovement(float tpf)
	{
		Node node = new Node();
		Quaternion rot = new Quaternion();
        Vector3f axis = new Vector3f( 0, 0, -1 ).normalizeLocal();
		rot.fromAngleNormalAxis( 15f * tpf, axis );
		
		node = (Node) this.model3D.getChild("RA");
		node = (Node) node.getChild("G1");
		node.getLocalRotation().multLocal(rot);
		
		node = (Node) this.model3D.getChild("RA");
		node = (Node) node.getChild("C1");
		node.getLocalRotation().multLocal(rot);
		
		node = (Node) this.model3D.getChild("RD");
		node = (Node) node.getChild("G2");
		node.getLocalRotation().multLocal(rot);
		
		node = (Node) this.model3D.getChild("RD");
		node = (Node) node.getChild("C2");
		node.getLocalRotation().multLocal(rot);
	}
	/*List<Spatial> root = node.getChildren();
	for(int i=0;i<root.size();i++)
	{
		System.out.println("CHILD: " + root.get(i).getName().toString());
	}*/
}
